<%@ include file="/common.jsp"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
		<title>酱出名门人事工资管理平台</title>
	</head>
	
  
  <body>
		<div class="main">
			<%@include file="/head.jsp"%>
			<div class="nav">
				<ul>
					<li>
						<a href="managerAction.action">操作员管理</a>
					</li>
					<li class="nav_pic"></li>
				    <li><a href="managerAction!jumpToUpdateManager.action?employeeId=${employeeId}startIndex=${startIndex}">编辑管理员</a></li>
	 				<li class="nav_pic"></li>
				</ul>
			</div>
		    
	 	    <div  id="tabs" style="float:left;width:200px;border:1px solid #e4e5e7;margin:10px 0;height:400px;" >

					<ul>
						<li><a href="#tab_role">角色分配</a></li>
						<li><a href="#tab_area">辖区分配</a></li>

					</ul>
					<div id="tab_role" >
						<ul >
						</ul>
					</div>
					<div id="tab_area" >
						<ul   style="list-style-type: none;margin: 0px;width: 100%">
						</ul>
					</div>

	 		</div>
	 		<div class="ui-corner-all right" style="height:404px;margin:10px 0;">
				 <form class="add_admin">
					<label for="dengluming">姓名：</label>
					<input type="text"  id="edit_name" class="text ui-widget-content ui-corner-all" style="height:30px;line-height:30px;" value="${manager.name}"><br />
					<label for="dengluming">登录名：</label>
					<input type="text" id="edit_user_name" class="text ui-widget-content ui-corner-all" style="height:30px;line-height:30px;" value="${manager.userName}"><br />
					<label for="mima">密码：</label>
					<input type="password" id="edit_pwd" class="text ui-widget-content ui-corner-all" style="height:30px;line-height:30px;" value="${password}"><br />
					<label for="mima">确认密码：</label>
					<input type="password" id="edit_verify_pwd" class="text ui-widget-content ui-corner-all" style="height:30px;line-height:30px;" value="${password}"><br />
					
					<input type="submit"  value="确定" style="width:50px;margin-left:300px;">
					<input type="reset" style="width:50px;margin-left:30px;">
					
	 			</form>
	 		</div>

	 						
			
     		<div class="clear"></div>
	 		<div class="footer">酱出名门人事工资管理平台 版权所有</div>
		</div>
  </body>
  <script type="text/javascript" >
  $(function() {
		
			//提交添加角色操作
			$(":submit").click(function () {
				var roleId = $("input[name='checkRole']:checked").val();
                var areaId = $("input[name='checkArea']:checked").val();
				var name = $.trim($("#edit_name").val());
				var userName = $.trim($("#edit_user_name").val());
				var pwd = $.trim($("#edit_pwd").val());
				var verifyPwd = $.trim($("#edit_verify_pwd").val());

                var alertMsg = "";
                if (name == "") {
                	alertMsg = "管理员姓名不能为空!";
                	$("#edit_name").focus();
                } else if (userName == "") {
                    alertMsg = "登录名不能为空!";
                    $("#edit_user_name").focus();
                } else if (pwd == "") {
                	alertMsg = "密码不能为空!"
                    $("#edit_pwd").focus();
                } else if (verifyPwd == "") {
                	alertMsg = "验证密码不能为空!";
                	$("#edit_verify_pwd").focus();
                } else if (verifyPwd != pwd) {
                	alertMsg = "密码不一致请重新输入密码!";
                	$("#edit_pwd").focus();
                } else if (typeof(roleId) == "undefined") {
                	alertMsg = "请为管理员选择一个角色!";
                	$( "#tabs" ).tabs("select", "#tab_role");
                }  else if (typeof(areaId) == "undefined") {
                	alertMsg = "请为管理员选择所辖区域!";
                	$( "#tabs" ).tabs("select", "#tab_area");
                } 
                
                if (alertMsg.length > 0) {
                    alert(alertMsg);
                    return false;
                }


				managerObjRef.roleId = roleId;
				managerObjRef.jurisdictions = areaId;
				managerObjRef.name = name;
				managerObjRef.userName = userName;
				var managerJson = JSON.stringify(managerObjRef);
				updateManager(managerJson, pwd);
				
				return false;
				
			});



	  
	  $( "#tabs" ).tabs();
	  $( "input:submit, input:reset").button();

	  /*$("input:text, input:password").each(function(index) {
		    $(this).val("");
       });*/
	  findManagerRole();
	  findAllArea();
	  
  });

  function updateManager(managerJson, pwd) {
  	  $.ajax({
		   type: "POST",
		   url: "managerAction!updateManager.action",
		   data: "managerJson=" + managerJson + "&password=" + pwd + "&timestamp=" + new Date().getTime(),
		   success: function(msg){
  				updateManagerCallback(msg);
		   },
		   error:function (textStatus) {
			   alert("网络异常请稍后重试!");
	       }
	  })
  }

  function updateManagerCallback(msg) {
      switch (msg.status.code) {
      case "SUCCES" :
          alert(msg.status.message);
      	  window.location.href="managerAction!jumpToMainManager.action?&startIndex=" + startIndex;
          break;
      case "FAIL" :
      	  alert(msg.status.message);
          break; 
      }
  }
  
  function findManagerRole() {
  	  $.ajax({
		   type: "GET",
		   url: "managerAction!findManagerRole.action",
		   success: function(msg){
  				findManagerRoleCallback(msg);
		   },
		   error:function (textStatus) {
			   alert("网络异常请稍后重试!");
 	       }
 	  })
  }

  function findManagerRoleCallback(msg) {
      switch (msg.status.code) {
      case "SUCCES" :
          showRoles(msg.datas);
          break;
      default :
      	 alert(msg.status.message);
         break; 
      }
  }
  
  function findAllArea() {
		$.ajax( {
			type : "GET",
			url : "areaAction!findAllArea.action",
			success : function(msg) {
				findAllAreaCallback(msg);
			},
			error : function(textStatus) {
				alert("网络异常请稍后重试!");
			}
		});
  }

  function findAllAreaCallback(msg) {
      switch (msg.status.code) {
      case "SUCCES" :
    	  showAreas(msg.datas);
    	  
          break;
      default :
      	 alert(msg.status.message);
         break; 
      }
  } 

  function showAreas(datas) {
	  $("#tab_area ul").html("");
	  
	  for ( var i = 0; i < datas.length; i++) {
          var li = $("<li ></li>"); 
		  var input = $("<input type='radio' name='checkArea'/>");
		  input.val(datas[i].areaId);
          if (datas[i].areaId == managerObjRef.jurisdictions) {
        	  input.attr("checked","checked");
          } 
		  
		  var label = $("<label></label>")
		  label.html(datas[i].name);
          li.append(input).append(label);
		  $("#tab_area ul").append(li);
	  }
	  
  }  


  function showRoles(datas) {
	  $("#tab_role ul").html("");

	  for ( var i = 0; i < datas.length; i++) {
		  var li = $("<li ></li>"); 
		  var input = $("<input type='radio' name='checkRole' />");
		  input.val(datas[i].roleId);
          if (datas[i].roleId == managerObjRef.roleId) {
        	  input.attr("checked","checked");
          } 
		  var label = $("<label></label>")
		  label.html(datas[i].name);
		  li.append(input).append(label);
		  $("#tab_role ul").append(li);
	  }
  }




  

  
  function managerObj() {
	  this.employeeId = "${manager.employeeId}";
	  this.employeeNum = "${manager.employeeNum}"
      this.roleId = "${manager.roleId}";
	  this.jurisdictions = "${manager.jurisdictions}";
      this.name = "";
      this.userName = "";
      
  }
  
  var managerObjRef = new managerObj();

  var startIndex = "${startIndex}";
  </script>
</html>
